﻿using System;
using System.Collections.Generic;
using System.Text;
using Infrastructure.ObjectModel;
using Infrastructure.ServiceInterfaces;
using Infrastructure.ObjectModel.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace Infrastructure.Managers
{
    public class SoundManager : GameService, ISoundManeger
    {
        // TODO 01: Declarations:
        AudioEngine m_AudioEngine;
        WaveBank m_WaveBank;
        SoundBank m_SoundBank;
        Cue m_Music;
        int m_MusicVolume = 10;
        int m_SoundVolume = 10;

        private float MusicVol
        {
            get
            {
                return m_MusicVolume / 10f;
            }
        }

        public int MusicVolume
        {
            get
            {
                return ((int)(m_MusicVolume * 10));
            }
        }

        public int SoundVolume
        {
            get
            {
                return ((int)(m_SoundVolume * 10));
            }
        }

        public String Mute
        {
            get
            {
                return (m_Mute ? "Off" : "On");
            }
        }

        public SoundManager(Game i_Game)
            // we want this component to be updated first!
            : base(i_Game)
        {
        }

        protected override void RegisterAsService()
        {
            Game.Services.AddService(typeof(ISoundManeger), this);
        }

        public override void Initialize()
        {
            // TODO 02: Instantiations and Loadings
            m_AudioEngine = new AudioEngine(@"Content\Audio\Invadors.xgs");
            m_WaveBank = new WaveBank(m_AudioEngine, @"Content\Audio\WaveBank.xwb");
            m_SoundBank = new SoundBank(m_AudioEngine, @"Content\Audio\SoundBank.xsb");

            // Holding a reference to our music 'cue' so we will be able
            // to control it during the game (pause/resume..)
            m_Music = m_SoundBank.GetCue("BGMusic");

            // start playing background music and then continut with loading the game:
            m_Music.Play();
        }

        public void DecreaseMusicVolume()
        {
            m_MusicVolume = (int)MathHelper.Clamp(m_MusicVolume - 1f, 0, 10);
            m_AudioEngine.GetCategory("Music").SetVolume(MusicVol);
        }

        public void IncreaseMusicVolume()
        {
            m_MusicVolume = (int)MathHelper.Clamp(m_MusicVolume + 1, 0, 10);
            m_AudioEngine.GetCategory("Music").SetVolume(MusicVol);
        }

        public void DecreaseSoundVolume()
        {
            m_SoundVolume = (int)MathHelper.Clamp(m_SoundVolume - 1, 0, 10);
            m_AudioEngine.GetCategory("Default").SetVolume(m_SoundVolume / 10f);
        }

        public void IncreaseSoundVolume()
        {
            m_SoundVolume = (int)MathHelper.Clamp((m_SoundVolume) + 1, 0, 10);
            m_AudioEngine.GetCategory("Default").SetVolume(m_SoundVolume / 10f);
        }

        private bool m_Mute = false;
        public void ToggleMute()
        {
            m_Mute = !m_Mute;
            m_AudioEngine.GetCategory("Music").SetVolume(m_Mute ? 0 : MusicVol);
            m_AudioEngine.GetCategory("Default").SetVolume(m_Mute ? 0 : m_SoundVolume / 10f);
        }

        public void PlayCue(string i_Cue)
        {
            Cue cue = m_SoundBank.GetCue(i_Cue);
            cue.Play();
        }
    }
}
